<template>
  <div>
    <el-card class="box-card center-card" style="margin-top:20px">
      <div class="text item">
        <el-upload
          class="upload-demo"
          ref="upload"
          :limit="1"
          accept=".zip"
          :headers="upload.headers"
          :action="upload.url"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
        >
          <el-button :loading="loading" slot="trigger" type="success" class="center-btn">上传表格
          </el-button>
          <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“zip”格式文件！</div>
        </el-upload>

        <div style="padding: 20px 0;">
          <i class="el-icon-bottom"></i>
        </div>

        <el-button type="primary" class="center-btn" @click="exportExcel">导出成绩表</el-button>
      </div>
    </el-card>
    <fail-list :dialogVisibleFu="dialogVisibleFu" :errorData="errorData" @closeDialog="closeDialog"></fail-list>
  </div>
</template>

<script>
  import {getToken} from "@/utils/auth";
  import {
    uploadHaiXiaoFolder,
    queryDataByHaiXiaoProcess,
    queryDataByHaiXiaoErr, dealToQingHuaData, getExcelZipExsitBK, getExcelZip
  } from "@/api/toolsystem/grade";
  import { downLoadZip } from "@/utils/zipdownload";
  import failList from "@/components/ToolSystem/Grade/failList";

  export default {
    name: "getGradeBiaoKao",
    components:{
      failList
    },
    data() {
      return {
        loading: false,
        percentage: 0,// 上传进度
        timer: null,// 定时器
        timerError: null,// 错误定时器
        // 上传表格参数
        upload: {
          // 是否禁用上传
          isUploading: false,
          // 设置上传的请求头部
          headers: {Authorization: "Bearer " + getToken()},
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/after-exam-score/dataReprocessing/uploadHaiXiaoFolder"
        },
        errorData:[], // 错误数据表格
        dialogVisibleFu: false, // 控制弹框显示与隐藏
      }
    },
    watch: {
      percentage(value) {
        if (this.percentage == 100) {
          this.loading = false;
          clearInterval(this.timerError);
          clearInterval(this.timer);
          setTimeout(() => {
            this.$root.globalDialog = false;
          }, 2000)
          this.msgSuccess('下载成功');
        }
      }
    },
    created() {
    },
    methods: {
      // 导出成绩表请求
      async exportExcelRq(){
        this.$confirm('确认要导出成绩表?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            downLoadZip('/after-exam-score/dataReprocessing/dealToQingHuaData', '成绩表');
          })
          .catch(()=>{
            this.msgInfo('取消操作')
          })
      },
      // 已经存在压缩包，返回压缩包
      getExcelZip(){
        downLoadZip('/after-exam-score/dataReprocessing/getExcelZip', '成绩表');
      },
      // 导出成绩表按钮
      exportExcel(){
        // 判断是否存在压缩包
        getExcelZipExsitBK().then(response => {
          if (response.code != 200) {
            return this.$message.error(response.msg);
          };
          if(response.data){
            this.getExcelZip()
          }else{
            this.exportExcelRq()
          }
        }).catch(error=>{
          return this.$message.error(error);
        })
      },

      // 关闭弹框
      closeDialog(val){
        this.dialogVisibleFu = val
      },
      // 上传初始表-文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.loading = true;
        this.upload.isUploading = true;
        this.percentage = 0
        this.$root.percentage = 0
        this.$root.globalDialog = true
        this.timer=setInterval(()=>{
          this.queryProcess()
        },10000)
        this.timerError = setInterval(()=>{
          this.queryInsertQuesErr();
        }, 10000)
      },
      // 上传初始表-文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.loading = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
      },
      // 上传初始表-获取进度
      queryProcess() {
        queryDataByHaiXiaoProcess().then(response => {
          if (response.code != 200) {
            return;
          };
          this.percentage = Number(response.data);
          this.$root.percentage = Number(response.data);
        }).catch(error => {
          console.log(error)
        })
      },
      // 上传初始表-查询生成表错误
      queryInsertQuesErr(){
        queryDataByHaiXiaoErr().then(response => {
          console.log(response);
          if (response.code != 200) {
            return;
          }
          if(response.data){
            clearInterval(this.timerError);
            clearInterval(this.timer);
            this.loading = false;
            this.upload.isUploading = false;
            this.$root.globalDialog = false;
            this.errorData = response.data;
            this.dialogVisibleFu = true;
          }
        }).catch(error=>{
          console.log(error)
        })
      },
    }
  }
</script>

<style scoped lang="scss">

</style>
